#ifndef YRMAINWINDOW_H
#define YRMAINWINDOW_H

#include <QMainWindow>
#include <QSqlDatabase>//连接数据库相关
#include <QSqlError>//数据库错误相关
#include <QSqlQuery>//执行sql语句相关
#include <QSqlRecord>
#include <QStandardItemModel>
#include <QStandardItem>

#include <QMessageBox>
#include <QDebug>//程序输出，相当于print

#include <QDialog>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QButtonGroup>
#include <QFormLayout>
#include <QCheckBox>
#include <QRadioButton>
#include <QFileDialog>
#include <QStandardPaths>
#include <QDesktopServices>

#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QStandardItemModel>//实现通用的二维数据的管理功能。

#include <QPointer>

#include <QToolTip>
#include <QMenu>

#include <QSettings>
#include <QTextCodec>

#include <QElapsedTimer>
#include <QTimer>
#include "ui_yrmainwindow.h"  // 确保包含这个头文件
#include "login.h"
#include "searchdialog.h"
#include "ui_searchdialog.h"
#include "database_home.h"
#include <unordered_map>
#include "qcustomplot.h"
class SearchDialog;
namespace Ui {
class YRMainWindow;
}

class YRMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit YRMainWindow(QWidget *parent = nullptr);
    ~YRMainWindow();

    void loadTestIDsToTreeWidget(const QString &queryString, bool showQueryTime = false);
    int update_count();

//重写closeEvent方法，实现关闭mainwindow的同时关闭非子类的showWidget
protected:
    void closeEvent(QCloseEvent *event) override;

private slots:
    void on_action_csv_triggered();

    void onTestIDClicked(QTreeWidgetItem *item, int column);

    void onTestIDClickedToDataDisplay(QTreeWidgetItem *item, int column);

    void onTestIDClickedToMainColumnDisplay(QTreeWidgetItem *item, int column);

    void draw(QString lineName,QVector<double> x,QVector<double> y,int lineNumber);
    void selectedShow();
    void contextMenuRequest(QPoint pos);
    void showGraph();
    void hideGraph();
    void savePlot();
    void clearPlot();
    void showPointToolTip(QMouseEvent *event);

    void on_action_three_csv_triggered();

    void on_pushButton_search_clicked();

    void on_pushButton_clicked();

    void on_main_column_to_csv_clicked();


    void on_blank_change_clicked(bool checked);

    void on_action_logout_triggered();

    void on_action_exit_triggered();

    void on_action_home_triggered();

    void on_action_dirpath_triggered();

    void on_pushButton_summary_clicked();

private:
    Ui::YRMainWindow *ui;

    QSqlDatabase yrDatabase;// 直接使用对象而不是指针

    QStandardItemModel *model;
    QStandardItemModel *data_model;

    QPointer<SearchDialog> dialog;  // 使用 QPointer 管理对话框

    void Init();

    // 建表
    bool checkAndCreateTable(QSqlDatabase *yrDatabase);

    bool importCSVToDatabase(const QString &filePath, QSqlDatabase *yrDatabase, bool isRedFile);
    bool importDetailedData(const QString &baseFilePath, const QString &redFilePath, QSqlDatabase *yrDatabase);

    QString extractTestIdFromFileName(const QString &filePath);

    void removeEmptyColumns(bool keepFirstColumn);

    void clearTableView();
    void readCSVData(QFile& file, QMap<int, QMap<QString, QString>>& dataMap, bool isRedFile);
    void readCSVDataDebug(QFile& file, QMap<int, QMap<QString, QVariant>>& dataMap, bool isRedFile);
    QString cleanHeaderName(const QString& originalHeader);
    QMap<QString, QString> getLineData(int timePoint, const QMap<int, QMap<QString, QString>>& fullData);

    void showTableContextMenu(const QPoint &pos);
    void updateSelectedItem();
    bool deleteTestIDFromDB();

    void showTableDataContextMenu(const QPoint &pos);
    void plotSelectedColumn();
    void exportBaseTableViewToCSV();
    void exportDetailTableViewToCSV();

    bool importDonglinDetailedData(const QString &baseFilePath, const QString &dataFilePath, QSqlDatabase *yrDatabase);
    void readDonglinCSVData(QFile& baseFile, QFile& dataFile, QMap<int, QMap<QString, QString>>& dataMap);

    void exportTableToCSV(QTableWidget *tableWidget);


    int countAllChildItems(QTreeWidgetItem *parentItem) const;

    QString engToChs(const QString &engName);

    void showMaterialStatistics();
};

#endif // YRMAINWINDOW_H
